package push;

import config.Config;
import file.FileManager;
import model.HTExtractItem;

import java.util.Arrays;
import java.util.List;

/**
 * Created by CatM on 18/1/30.
 */
public class PushManager {
    public static PushManager INSTANCE = new PushManager();

    public static PushManager get() {
        return INSTANCE;
    }

    public void push(HTExtractItem items) {
        if(items != null){
            push(Arrays.asList(items));
        }
    }

    public void push(List<HTExtractItem> items) {
        if(items == null || items.size() == 0 ) return;
        for (HTExtractItem item : items) {
            int pushCount = FileManager.get().getInt(item.url);
            if(pushCount != -1 && pushCount >= Config.PUSH_COUNT){
                System.out.println("已经发送过Push了，itemName="+item.name + "; url="+item.url);
                continue;
            }
            if(pushCount == -1){
                pushCount = 0;
            }

            pushCount++;

            GTPush gtPush = new GTPush();
            gtPush.push(item);
            System.out.println("发送了一条Push，itemName=" + item.name + "; url=" + item.url);
            FileManager.get().edit().putInt(item.url, pushCount);
        }
    }
}
